package com.wangtao.firstapp.activity.sife_secondactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.ACTSifeSecondContentAdapter;
import com.wangtao.firstapp.adapters.ACTSifeSecondLookAdapter;
import com.wangtao.firstapp.adapters.ACTSifeSecondReviewAdapter;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.modles.CommentsListData;
import com.wangtao.firstapp.modles.OneSifeSecondLook;
import com.wangtao.firstapp.modles.TwoSifeSecond;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 二级页面-->小唐君有图页面
 * Created by lanouhn on 16/8/29.
 */
public class TwoSifeSecondActivity extends Activity implements View.OnClickListener {

    private ImageButton back, ivshare;
    private TextView tvcomment;

    //文章，图，卖东西
    private TwoSifeSecond twoSifeSecond;
    private RecyclerView recyclerViewTwoSecond;
    private ACTSifeSecondContentAdapter actSifeSecondContentAdapter;

    //大家都在看
    private OneSifeSecondLook oneSifeSecondLook;
    private RecyclerView recycierView_look;
    private ACTSifeSecondLookAdapter actSifeSecondLookAdapter;


    //评论
    private RecyclerView recycierView_review_content;
    private ACTSifeSecondReviewAdapter actSifeSecondReviewAdapter;
    private CommentsListData commentsListData;

    private ScrollView mRootScrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actsifesecond_two);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mRootScrollView= (ScrollView) findViewById(R.id.scScrollView_actSifeSecond_two);

        back = (ImageButton) findViewById(R.id.back_actSifeSecond_two);
        ivshare = (ImageButton) findViewById(R.id.ivshare_actSifeSecond_two);
        tvcomment = (TextView) findViewById(R.id.tvcomment_actSifeSecond_two);

        recyclerViewTwoSecond = (RecyclerView) findViewById(R.id.recycierView_actSifeSecond_two);
        recycierView_look = (RecyclerView) findViewById(R.id.recycierView_look);
        recycierView_review_content = (RecyclerView) findViewById(R.id.recycierView_review_content);

    }

    private void initData() {

        Intent intent = getIntent();
        //文章，图，卖东西
        String item_ID = intent.getStringExtra("item_ID");
        String urlSecond = "http://open4.bantangapp.com/topic/newInfo";
        String paramsSecond = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";

        try {
            HttpUtils.doPostAsyn(urlSecond, paramsSecond, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_TwoSecond = new Gson();
                    twoSifeSecond = gson_TwoSecond.fromJson(result, TwoSifeSecond.class);
                    if (twoSifeSecond.getData().getProduct_list().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });

            //大家都在看
             String urlLook = "http://open4.bantangapp.com/topics/topic/relationRec";
             String paramsLook = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID;

            HttpUtils.doPostAsyn(urlLook, paramsLook, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_look = new Gson();
                    oneSifeSecondLook = gson_look.fromJson(result, OneSifeSecondLook.class);
                    if (oneSifeSecondLook.getData().getTopic().size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });


            //评论
             String urlReview = "http://open4.bantangapp.com/topics/comment/list";
             String paramsReview = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=20&id=" + item_ID;

            HttpUtils.doPostAsyn(urlReview, paramsReview, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_Review = new Gson();
                    commentsListData = gson_Review.fromJson(result, CommentsListData.class);
                    if (commentsListData.getData().size() > 0) {
                        handler.sendEmptyMessage(3);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    tvcomment.setText(twoSifeSecond.getData().getComments() + "评论");
                    actSifeSecondContentAdapter = new ACTSifeSecondContentAdapter(TwoSifeSecondActivity.this, twoSifeSecond);
                    recyclerViewTwoSecond.setAdapter(actSifeSecondContentAdapter);

                    recyclerViewTwoSecond.setLayoutManager(new FullyLinearLayoutManager(TwoSifeSecondActivity.this));
                    break;
                case 2:
                    actSifeSecondLookAdapter = new ACTSifeSecondLookAdapter(TwoSifeSecondActivity.this, oneSifeSecondLook);
                    recycierView_look.setAdapter(actSifeSecondLookAdapter);
                    recycierView_look.setLayoutManager(new FullyLinearLayoutManager(TwoSifeSecondActivity.this));
                    break;
                case 3:
                    actSifeSecondReviewAdapter = new ACTSifeSecondReviewAdapter(TwoSifeSecondActivity.this, commentsListData);
                    recycierView_review_content.setAdapter(actSifeSecondReviewAdapter);
                    recycierView_review_content.setLayoutManager(new FullyLinearLayoutManager(TwoSifeSecondActivity.this));
                    break;
            }
        }
    };

    private void initEvent() {
        back.setOnClickListener(this);
        ivshare.setOnClickListener(this);
        tvcomment.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回上一个页面
            case R.id.back_actSifeSecond_two:
                finish();
                break;
            //分享
            case R.id.ivshare_actSifeSecond_two:
                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
                break;
            //直达评论处位置
            case R.id.tvcomment_actSifeSecond_two:
                mRootScrollView.fullScroll(ScrollView.FOCUS_DOWN);
               // Toast.makeText(this, "直达评论处位置", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}